package org.lc.eic.messagingsystem.activemq;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.lc.eic.messageendpoint.exception.FailedProcessMessageException;

public class InboundEndpoint implements
		org.lc.eic.messageendpoint.InboundEndpoint {
	
	private List<byte[]> receivedMessages = new ArrayList<byte[]>(); 

	public void onMessage(InputStream inStream) throws FailedProcessMessageException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		
		byte[] buffer = new byte[1024];
		int read;
		
		try {
			while(true){
				read = inStream.read(buffer);
				if(read == -1){
					break;
				}
				baos.write(buffer, 0, read);
			}
			
			baos.close();
			inStream.close();
			
			receivedMessages.add(baos.toByteArray());
		} catch (IOException e) {
			throw new FailedProcessMessageException("Failed process message. ", e);
		}

	}

	public List<byte[]> getReceivedMessages() {
		return receivedMessages;
	}

}
